package com.powernode.springboot.conttroller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    MessageSource messageSource;

    @GetMapping("resource/{id}")
    public String getResource(@PathVariable("id") Long id) {
        if (id == 1) {
            throw new IllegalArgumentException("无效ID：" + id);
        }
        return "ID：" + id;
    }

    //局部生效
    @ExceptionHandler(value = {IllegalArgumentException.class})
    public String handlerIllegalArgumentException(IllegalArgumentException e) {
        return "错误信息：" + e.getMessage();
    }

    @GetMapping("/welcome")
    public String getWelcome(HttpServletRequest request) {
        String message = messageSource.getMessage("welcome.message", null, request.getLocale());
        return message;
    }
}
